<template>
    <view v-if="state.show" class="level_container">
        <Info :data="state" />
        <Rule :data="state" />
    </view>
</template>

<script setup>
import Info from './info.vue';
import Rule from './rule.vue';
import { getImgOssUrl } from '@/commons/utils';
import request from '@/request';
import { onShow } from '@dcloudio/uni-app';
import { reactive, inject } from 'vue';

const currency = inject('currency');
const state = reactive({
    show: false,
    avatar: '', //头像
    nickname: '', //昵称
    levelName: '', //当前等级名称
    nextLevelName: '', //下一等级名称
    nextLevelObj: {
        mobileFlag: 1, //手机号登录,
        applyDtbor: '', //申请分销员条件 1-累计购买金额 2-累计购买次数
        amountOrNum: '', //累计购买金额(分)或购买次数   前三个字段等级1专有
        addToPlug: '', //(累计推广金额分)" 第一等级暂无后面三项
        bindFansNum: '', //(绑定粉丝数量)"
        inviteDtbNum: '' //(邀请分销员数量)"
    }, // 下一等级对象
    amountOrNum: '', //累计购买金额(分)或购买次数   前两个字段等级1专有
    addToPlug: '', // 累计推广金额分
    bindFansNum: '', // 累计粉丝数
    inviteDtbNum: '', // 邀请分销员数
    /* 还差 */
    shortAddToPlug: '', //还差(累计推广金额分)
    shortFans: '', //还差粉丝数
    shortInviteDtb: '', //还差邀请分销员数
    shortAmountOrNum: '', // 还差购买金额(分)或购买次数
    inviteSwitch: false, //邀请佣金开关 1-关闭2-开启
    plugScale: '', //推广奖励比例
    inviteScale: '', //邀请奖励比例
    levelRule: [], //等级规则
    highLevel: false //是否最高等级
});
const getData = async () => {
    const { data } = await request('dtbCenterServ');
    state.avatar = data.avatar || getImgOssUrl('/ui-prod-2.7.0/avatar.png');
    state.nickname = data.nickname || '默认昵称';
    state.levelName = data.levelName;
    state.nextLevelName = data.nextLevelName;
    state.levelRule =
        data.levelCenter.levelRule?.map((item, index) => {
            item.plugScale = currency(item.plugScale).divide(100).value;
            item.inviteScale = currency(item.inviteScale).divide(100).value;
            // 一级等级
            if (index === 0) {
                item.ruleArr = [];
                if (item.upgradeConfig.applyDtbor === 0) {
                    item.ruleArr.push('手机号登录自动成为该等级');
                } else if (item.upgradeConfig.applyDtbor === 1) {
                    item.ruleArr.push(`推广金额累计￥${currency(item.upgradeConfig.amountOrNum).divide(100)}元`);
                } else {
                    item.ruleArr.push(`累计购买${item.upgradeConfig.amountOrNum}次`);
                }
                // 非一级等级
            } else {
                item.ruleArr = [];
                if (item.upgradeConfig.addToPlug) {
                    item.ruleArr.push(`推广金额累计￥${currency(item.upgradeConfig.addToPlug).divide(100)}元`);
                }
                if (item.upgradeConfig.bindFansNum) {
                    item.ruleArr.push(`累计绑定粉丝${item.upgradeConfig.bindFansNum}个`);
                }
                if (item.upgradeConfig.inviteDtbNum) {
                    item.ruleArr.push(`累计邀请分销员${item.upgradeConfig.inviteDtbNum}个`);
                }
            }
            return item;
        }) || [];

    state.addToPlug = data.levelCenter.upgradeCondition.addToPlug;
    state.bindFansNum = data.levelCenter.upgradeCondition.bindFansNum;
    state.inviteDtbNum = data.levelCenter.upgradeCondition.inviteDtbNum;
    state.nextLevelObj = state.levelRule.find(item => item.dtbLevelName === state.nextLevelName)?.upgradeConfig || {};
    state.shortAddToPlug =
        state.nextLevelObj.addToPlug && currency(state.nextLevelObj.addToPlug).subtract(state.addToPlug).divide(100);
    state.shortFans = currency(state.nextLevelObj.bindFansNum).subtract(state.bindFansNum).value;
    state.shortInviteDtb = currency(state.nextLevelObj.inviteDtbNum).subtract(state.inviteDtbNum).value;
    state.inviteSwitch = data.levelCenter.inviteSwitch == 2;
    state.plugScale = currency(data.levelCenter.plugScale).divide(100).value || 0;
    state.inviteScale = currency(data.levelCenter.inviteScale).divide(100).value || 0;
    state.highLevel = data.highLevel == 1;
    state.show = true;
};
onShow(() => {
    getData();
});
</script>

<style lang="scss" scoped>
.level_container {
}
</style>
